﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using HuberRace.BusinessLayer;

namespace HuberRace
{
    public partial class OpenRace : Form
    {
        Main m_myParent = null;

        public OpenRace(Main myParent)
        {
            InitializeComponent();
            m_myParent = myParent;
        }

        private void OpenRace_Load(object sender, EventArgs e)
        {
            m_myParent.EstablishSQLConnection();

            SqlDataAdapter da = new SqlDataAdapter("SELECT (CAST(date AS VARCHAR(11)) + ' - ' + Name) as displayName, raceId FROM Races", m_myParent.cn);
            DataTable dt = new DataTable();
            da.Fill(dt);

            raceList.DataSource = dt;
            raceList.DisplayMember = "displayName";
            raceList.ValueMember = "raceId";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void openRace(int raceId)
        {
            // The method OpenRace in the Main class is responsible for
            // opening the race and binding it to the UI. Consider
            // having this done by a method in the Program class...
            m_myParent.OpenRace(raceId);
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            openRace((int)raceList.SelectedValue);
            Close();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            openRace((int)raceList.SelectedValue);
            Close();
        }
    }
}
